using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Awaiter;
using Nsb.Classes;

namespace PlaySpace.Classes
{
    [IgnoreNamespace]
    public static class SiteHelpers
    {
        public static void ForVersion(string site, Action<string,string> fn)
        {
            string nm = "Vx" + site;
            string url = Uri.Join("../spaces", site, "Scripts/Scripts/version.txt");
            if (site == "root")
                url = "../Scripts/Scripts/version.txt";
            else if (site.IndexOf("../") == 0)
                url = Uri.Join(site, "Scripts/Scripts/version.txt");
            new Await()
                .LoadJson(nm, Uri.Uniqueify(url))
                .HandleDl(Exceptions.BlobLoadError, Await.FinishAw)
                .AddDx(
                    delegate
                    {
                        Dictionary<string, int> vx = (Dictionary)Await.Files[nm];
                        fn(site, String.Format("{0}.{1}.{2}", vx["version"], vx["revision"], vx["build"]));
                        Await.Files.Remove(nm);
                    })
                .Commit();
        }
    }
}
